package com.livestocksolutions.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

/* -- Version History --
 *  1.0 : 06 August 2012 - Initial version.
 *  1.1 : 03 September 2012 - Changed context from type Context to child class BaseActivity.
 */

/**
 * A class to create the layout for the list of observations
 * @author Adam Parr
 * @version 1.1
 * 
 * @.modified 19 October 2012
 */
public class ListObservationArrayAdapter extends ViewObservationArrayAdapter {
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Contacts to represent in the ListView.
	 */
	public ListObservationArrayAdapter(BaseActivity context, String[][] values) {
		super(context, android.R.layout.simple_list_item_1, values);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		setView(R.layout.list_item_observation);
		final View rowView = super.setView(position, convertView, parent);
		
		TextView date = (TextView) rowView.findViewById(R.id.observationDate);
		TextView type = (TextView) rowView.findViewById(R.id.observationType);
		TextView description = (TextView) rowView.findViewById(R.id.observationPreview);

		// Create this array to be the full length that will be needed to avoid ArrayIndexOutOfBoundsException
		if(values[position].length<=VALUE){
			String[] orig = values[position];
			values[position] = new String[VALUE+1];
			int x = 0;
			for(String s : orig){
				values[position][x]=s;
				x++;
			}
		}
		
		// Set the current values of the Observation object
		final String[] array = context.getResources().getStringArray(R.array.observation_type);
		
		date.setText(context.format(context.parse(values[position][DATE])));
		type.setText(array[Integer.parseInt(values[position][TYPE])]);
		description.setText(values[position][VALUE]);
		return rowView;
	}
}
